/*
 * File:   form_scaling.cpp
 * Author: dima
 *
 * Created on 26 февраля 2015 г., 0:29
 */
#include <QMessageBox>
#include "form_scaling.h"
#include "ui_form_scaling.h"
#include "ipd_scaling.h"
#include "ipd_exception.h"

form_scaling::form_scaling(ipd::ipd_img& image) : widget(new Ui::form_scaling())
{
    widget->setupUi(this);
    convertible_image = new ipd::ipd_img();
    scaling = new ipd::ipd_scaling(image);

}

form_scaling::~form_scaling()
{
    delete convertible_image;
    delete scaling;
    delete widget;
}

void form_scaling::scaling_image()
{

    widget->convert->setEnabled(false);
    try
    {
        int scale = widget->sz_scale->value();
        if (widget->bilinear_interpolation->isChecked())
        {
            double sc = scale;
            if (!widget->is_increase->isChecked())
                sc = 1 / sc;
            scaling->bilinear_interpolation(*convertible_image, (float) sc);
        }
        else
        {
            if (widget->is_increase->isChecked())
                scaling->duplicating_pixels(*convertible_image, scale, true);
            else
                scaling->duplicating_pixels(*convertible_image, scale, false);
        }

    }
    catch (ipd::ipd_exception &except)
    {
        QMessageBox::critical(0, QString::fromUtf8(except.in_file().c_str()), QString::fromUtf8(except.what().c_str()), 0, 0);
    }
    widget->convert->setEnabled(true);
    emit send_image(*convertible_image);
}

void form_scaling::pos_slider(int index)//get pos slider
{
    widget->label->setText(QString::fromUtf8("Scale ") + QString::number(index) + QString::fromUtf8(" :"));
}
